// Copyright (C) 2010  Joan Puigcerver Perez, <joapuipe@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#ifndef __SAXPEOPLEHANDLER_HPP__
#define __SAXPEOPLEHANDLER_HPP__

#include <xercesc/sax2/DefaultHandler.hpp>
#include <xercesc/sax2/Attributes.hpp>

#include "DBManager.hpp"
#include <string>

#ifdef XERCES_CPP_NAMESPACE_USE
XERCES_CPP_NAMESPACE_USE
#endif

/**
 * \class SAXPeopleHandler
 *
 * This function is used to handle the actions of the XML parser when the people XML is being parsed.
 * Its behaviour is similar to SAXMoviesHandler and SAXRequestsHandler.
 * 
 * \see People
 * \see SAXMoviesHandler
 * \see SAXRequestsHandler
 */
class SAXPeopleHandler: public DefaultHandler {
private:
  XMLCh * _kname;

  DBManager& db;
  std::string curr_person;
  std::string curr_work;
  bool proc_aka;//, proc_actor, proc_director, proc_producer, proc_writer;
  typedef std::basic_string<XMLCh> XercesString;
  XercesString buffer;
  
  /**
   * This function tries to create a new Person in the database and returns a pointer to it.
   * If the person could not have been created, it returns a null pointer.
   * 
   * @param name Person's name.
   */
  std::string createPerson(const XMLCh * name);
  
  /**
   * Add an aka name to the current person.
   *
   * @param aka Aka name.
   */
  void addAka(const XercesString& aka);

  /**
   * Add a link between the current person and a movie.
   *
   * @param movie The title of the movie to link.
   * @param work  The work of the person in the movie.
   */
  void addLink(const XercesString& movie, std::string work);
public:
  /**
   * Constructor.
   * 
   * @param database The database where the data will be stored.
   */
  SAXPeopleHandler(DBManager& database) : db(database), curr_person(""), curr_work("") {};

  /** This function is executed by the parser when the document starts. */
  void startDocument ();

  /** This function is executed by the parser when the document ends. */
  void endDocument ();

  /** 
   * Receive notification of the start of an element.
   *
   * @param uri The URI of the associated namespace for this element
   * @param localname The local part of the element name
   * @param qname The QName of this element
   * @param attrs The specified or defaulted attributes. 
   */
  void startElement ( const XMLCh* const uri,
		      const XMLCh* const localname,
		      const XMLCh* const qname,
		      const Attributes&  attrs );

  /** 
   * Receive notification of the end of an element.
   *
   * @param uri The URI of the associated namespace for this element
   * @param localname The local part of the element name
   * @param qname The QName of this element
   */
  void endElement( const XMLCh* const uri, 
		   const XMLCh* const localname,
		   const XMLCh* const qname );
  
  /** 
   * Receive notification of character data inside an element.
   *
   * @param chars The characters.
   * @param length The number of caracters to use from the character array.
   */
  void characters( const XMLCh* const chars,
		   const XMLSize_t length );

  /**
   * Report a fatal XML parsing error. 
   * 
   * @param exception The error information encoded as an exception.
   */
  void fatalError( const SAXParseException& exception );

  /**
   * Report a XML parsing error, usually it happens when the XML does not follow the schema.
   * 
   * @param exception The error information encoded as an exception.
   */  
  void error(const SAXParseException &exception);
};

#endif
